package com.ruoyi.answer.controller;

import com.ruoyi.answer.common.base.BaseAppController;
import com.ruoyi.answer.common.exception.UnauthorizedException;
import com.ruoyi.answer.common.page.ResultData;
import com.ruoyi.answer.entity.AdminUser;
import com.ruoyi.answer.service.AdminUserService;
import com.ruoyi.common.utils.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
public class LoginController extends BaseAppController {
    public  AdminUserService adminUserService;

    @PostMapping("/login")
    public ResultData login(@RequestBody AdminUser loginUser) {
        AdminUser appUser = adminUserService.findByUsername(loginUser.getAccount());
        // 这里是演示，正式使用要加密
        if (appUser.getPassword().equals(loginUser.getPassword())) {
            return ResultData.success(JwtUtil.sign(loginUser.getAccount()));
        } else {
            throw new UnauthorizedException();
        }
    }

    @GetMapping("/getUserId")
    public ResultData getUserId() {
        return ResultData.success(getCurrentUserId());
    }
}
